/**
 * @file 评价列表配置
 * @author yangjiangming <449654337@qq.com>
 * @date 2019/01/15 16:18:52
 */

layui.extend({
    oms_common_table_config: 'common/zmn.oms.common.table.config'
}).define(['$http', '$api', 'zmnUtils', 'oms_common_table_config'], function (exports) {

    var $http = layui.$http;
    var $api = layui.$api;
    var zmnUtils = layui.zmnUtils;

    var tableConfigObj = layui.oms_common_table_config;
    var tableConfig = tableConfigObj.createTableConfig('orderPurpose');

    var operateIdEnum = {
        home: 103
    };

    var MODULE_ID = 580;

    var operateConfig = {
        moduleId: MODULE_ID,
        primaryKey: 'orderId',
        fields: ['workId', 'orderId'],
        sysBaseURL: OMS_BASE_URL,
        operateListURL: '/order/purpose/operate/all/permission.action',
        operateObjectURL: '/order/purpose/operate/one/permission.action',
        operationList: [
            { id: 11, name: '售卖', special: true, permitID: '/zs/order_purpose/modify/sale.action', url: '/order_purpose/modify/sale.action' },
            { id: 12, name: '下单', special: true, permitID: '/zs/order_purpose/modify/createOrder.action', url: '/order_purpose/modify/createOrder.action' },
        ],
        url: {
        },
        operateIdEnum: operateIdEnum,
        ui: {
            header: true,
            preAndNext: true
        },
        end: function () {
            window.updateRowDataByWorkId();
        },
        specialHandler: function (params, opConfigObj) {
            specialHandler(params, opConfigObj);
        },
        paramsParser: function (rowData) {
            return {
                orderPurposeId: rowData.orderPurposeId
            };
        }
    };

    /**
     * 打开新tab页
     * @param url
     * @param text
     * @private
     */
    function _openNewTab (url, text) {
        top.layui.index.openTabsPage(url, text);
    }
    function checkOrderPurposePermiss (opConfigObj) {
        return $api.permitCheck({ permit: opConfigObj.id, moduleId: MODULE_ID });
    }

    function saleOrderPurpose (params, opConfigObj) {
        checkOrderPurposePermiss(opConfigObj).then(function () {
            layer.confirm('是否标记为售卖', { icon: 3, title:'提示' }, function (index) {
                $http.lockFetch('/order/purpose/modify/sale.action', params).then(function () {
                    zmnUtils.layui.toastSuccess('售卖成功');
                }, function (msg) {
                    zmnUtils.layui.toastError('售卖失败，原因：' +  msg);
                });

                layer.close(index);
            });
        }).fail(function () {
            zmnUtils.layui.toastError('无操作权限');
        });
    }
    function createOrder (params, opConfigObj) {
        checkOrderPurposePermiss(opConfigObj).then(function () {
            _openNewTab(OMS_BASE_URL+'/order/normal/purposeAdd.action?orderPurposeId='+params.orderPurposeId,'添加工单');
        }).fail(function () {
            zmnUtils.layui.toastError('无操作权限');
        });
        //_openNewTab('/order/normal/purposeAdd.action?orderPurposeId='+params.orderPurposeId,'添加工单');
    }

    function specialHandler (params, opConfigObj) {
        switch (params.operateId) {
            case 11:
                saleOrderPurpose(params, opConfigObj);
                break;
            case 12:
                createOrder(params, opConfigObj);
                break;
        }
    }

    exports('oms_purpose_config', {
        tableConfig: tableConfig,
        operateConfig: operateConfig
    });
});
